package com.example.concurrentprogramming8016.disruptor.produceconsumer.singleproduceconsumer;

import com.lmax.disruptor.RingBuffer;

/**
 * @author liyanhao
 * @Date 2023-01-08 11:19
 */
public class OrderEventProducer {
    //事件队列
    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(long value, String name) {
        //获取事件队列的下一个槽位
        long sequence = ringBuffer.next();
        try {
            //获取消息 事件
            OrderEvent orderEvent = ringBuffer.get(sequence);
            //写入消息数据
            orderEvent.setValue(value);
            orderEvent.setName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println("生产者发送数据value:" + value + ",name" + name);
            //发送事件
            ringBuffer.publish(sequence);
        }
    }
}
















